package com.smartlifev30.baiwei.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.smartlifev30.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 忘记密码和注册共用同一个界面(title文字不同),功能一样
 *
 * @author tys
 */
public class RegAndForPwdActivity extends AppCompatActivity {

    /**
     * 查看用户协议 请求码  判断是否点击了同意协议
     */
    private static final int USER_AGREEMENT = 100;

    /**
     * 注册
     */
    public static final int FLAG_REGISTER = 0x0;

    /**
     * 忘记密码
     */
    public static final int FLAG_FORGET_PWD = 0x1;

    @BindView(R.id.visible_password_iv)
    ImageView visiblePwdIv;

    @BindView(R.id.phone_num)
    EditText phoneNum;

    @BindView(R.id.check_code)
    EditText checkCode;

    @BindView(R.id.pwd)
    EditText pwd;

    @BindView(R.id.sure_pwd)
    EditText surePwd;

    @BindView(R.id.accept_agreement_cb)
    CheckBox acceptAgreementCb;

    /**
     * 忘记密码或注册
     */
    @BindView(R.id.title_them)
    TextView titleThemTv;

    /**
     * 用户协议LinerLayout 忘记密码时隐藏
     */
    @BindView(R.id.agreement_ll)
    View agreementLl;

    /**
     * 注册 或 确定
     */
    @BindView(R.id.register)
    Button register;


    private boolean isVisiblePwd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        int flag = getIntent().getIntExtra("flag", -1);
        switch (flag) {
            //注册
            case FLAG_REGISTER:
                setRegisterUi();
                break;
            //忘记密码
            case FLAG_FORGET_PWD:
                setForgetPwdUi();
                break;
            default:
        }
    }

    private void setRegisterUi() {
        titleThemTv.setText(R.string.register);
        register.setText(R.string.register);
        agreementLl.setVisibility(View.VISIBLE);
    }

    private void setForgetPwdUi() {
        titleThemTv.setText(R.string.forget_pwd);
        register.setText(R.string.sure);
        agreementLl.setVisibility(View.GONE);
    }

    @OnClick(R.id.register)
    void register() {
        Toast.makeText(this, "注册", Toast.LENGTH_SHORT).show();
    }


    @OnClick(R.id.getCheckCode)
    void getCheckCode() {
        Toast.makeText(this, "获取验证码", Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.agreement)
    void agreement() {
        Intent intent = new Intent(this, UserAgreementActivity.class);
        startActivityForResult(intent, USER_AGREEMENT);
    }


    @OnClick(R.id.visible_password_fl)
    void visiblePwd(View view) {
        if (isVisiblePwd) {
            visiblePwdIv.setImageResource(R.drawable.eye_gray);
            pwd.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
        } else {
            visiblePwdIv.setImageResource(R.drawable.eye_black);
            pwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
        isVisiblePwd = !isVisiblePwd;
    }

    @OnClick(R.id.forget_back)
    void onBack() {
        onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            //用戶协议
            case USER_AGREEMENT:
                if (resultCode == 'O') {
                    acceptAgreementCb.setChecked(true);
                }
                break;
            default:
        }
    }
}
